package com.telemetrymng.activity;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Typeface;
import android.os.Bundle;

import com.androidplot.ui.SizeLayoutType;
import com.androidplot.ui.SizeMetrics;
import com.androidplot.xy.BoundaryMode;
import com.androidplot.xy.LineAndPointFormatter;
import com.androidplot.xy.PointLabelFormatter;
import com.androidplot.xy.SimpleXYSeries;
import com.androidplot.xy.XYStepMode;
import com.telemetrymng.TelemetryManager;
import com.telemetrymng.graphic.XYPlotZoomPan;

public class RPMVisualization extends Activity {
	
	private XYPlotZoomPan xyPlot;
	private String parameterSelected;
	private List<List<String>> valuesByLap;
	private int laps;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_parameter_visualization);
		
		//referencia a la grafica
        xyPlot = (XYPlotZoomPan) findViewById(R.id.xyPlot);
		
		//parametro seleccionado
		parameterSelected = (String) getIntent().getExtras().get("SelectedParam");
		
		//numero de vueltas
        laps = Integer.parseInt(Collections.max(TelemetryManager.getDisplay().getTelemetry().getTelemetryValues().get("Lap")));
        
        SimpleXYSeries xySeries = null;
		List<String> strLapValues = null;
		if (parameterSelected.equals("RPM")) {
			//Para cada vuelta
			for (int i = 0; i < laps; i++) {
				List<Integer> intLapValues = new ArrayList<Integer>();
				//Si es la primera
				if (i == 0) {
					LineAndPointFormatter format = new LineAndPointFormatter(
						Color.rgb(0, 153, 153),                 		// line color
						null,               							// point color
						null,											// fill color (none)
						new PointLabelFormatter(Color.TRANSPARENT));  	// value label
					//incluimos el valor 0 para la muestra inicial de la vuelta
					intLapValues.add(0);
					strLapValues = TelemetryManager.getParamValuesByLap(parameterSelected, i+1);
					for (int j = 0; j < strLapValues.size(); j++) {
						intLapValues.add(Double.valueOf(strLapValues.get(j)).intValue());
					}
					xySeries = new SimpleXYSeries(
						intLapValues,       					 // array => list
						SimpleXYSeries.ArrayFormat.Y_VALS_ONLY,  // Y_VALS_ONLY means use the element index as the x value
						String.valueOf(i+1));					 // Title of this series
		        	xyPlot.addSeries(xySeries, format);

				} else {
					//incluimos el valor de la ultima muestra de la vuelta anterior como valor inicial de vuelta
					intLapValues.add(Double.valueOf(strLapValues.get(strLapValues.size()-1)).intValue());
					strLapValues.clear();
					strLapValues = TelemetryManager.getParamValuesByLap(parameterSelected, i+1);
					//En la primera vuelta incluimos el valor 0 para la muestra 0
					for (int j = 0; j < strLapValues.size(); j++) {
						intLapValues.add(Double.valueOf(strLapValues.get(j)).intValue());
					}
					LineAndPointFormatter format = new LineAndPointFormatter(
						Color.MAGENTA,                 					// line color
						null,               							// point color
						null,											// fill color (none)
						new PointLabelFormatter(Color.TRANSPARENT));  	// value label
					xySeries = new SimpleXYSeries(
						intLapValues,       					 // array => list
						SimpleXYSeries.ArrayFormat.Y_VALS_ONLY,  // Y_VALS_ONLY means use the element index as the x value
						String.valueOf(i+1));					 // Title of this series
			        	xyPlot.addSeries(xySeries, format);
			    }
			}
		}
		
	    // Fuente valores xy
        Typeface tfXY = Typeface.createFromAsset(getAssets(), "LED.ttf");
        
        //debug padding & margins
        //xyPlot.setMarkupEnabled(true);
        xyPlot.setPlotPadding(-10, 0, 0, 0);
           
        
		xyPlot.setTitle("RPM VS TIME");
		xyPlot.getTitleWidget().getLabelPaint().setTypeface(tfXY);
		xyPlot.getTitleWidget().getLabelPaint().setTextSize(16);
		xyPlot.getTitleWidget().getLabelPaint().setColor(Color.WHITE);
       
        
        //Leyenda
        xyPlot.getLegendWidget().setHeight(14, SizeLayoutType.ABSOLUTE);
        
        /******************************Ejes*******************************/
        
        /******************************Eje X******************************/
        //Etiqueta eje x
        xyPlot.setRangeLabel("");
//        xyPlot.getRangeLabelWidget().getLabelPaint().setTypeface(tfXY);
//        xyPlot.getRangeLabelWidget().getLabelPaint().setTextSize(17);

        // Formato valores
        xyPlot.setDomainValueFormat(new DecimalFormat("#####.##"));
        // Limites 
        xyPlot.setDomainBoundaries(0.0f, 10.5f, BoundaryMode.FIXED);
        // Paso
        xyPlot.setDomainStep(XYStepMode.INCREMENT_BY_VAL, 1);
     	// Ticks
        xyPlot.setTicksPerDomainLabel(1);
        // Color
        xyPlot.getGraphWidget().getDomainOriginLinePaint().setColor(Color.DKGRAY);
        // Ancho 
        xyPlot.getGraphWidget().getDomainOriginLinePaint().setStrokeWidth(2);
        //Fuente
        xyPlot.getGraphWidget().getDomainOriginLabelPaint().setTextSize(15);
        xyPlot.getGraphWidget().getDomainLabelPaint().setTextSize(15);
        xyPlot.getGraphWidget().getDomainOriginLabelPaint().setTypeface(tfXY);
        xyPlot.getGraphWidget().getDomainLabelPaint().setTypeface(tfXY); 
        xyPlot.getGraphWidget().getDomainOriginLabelPaint().setColor(Color.WHITE);
        xyPlot.getGraphWidget().getDomainLabelPaint().setColor(Color.WHITE);
        
        /******************************Eje Y******************************/
        // Etiqueta eje Y
        xyPlot.setDomainLabel("");
//        xyPlot.getDomainLabelWidget().getLabelPaint().setTypeface(tfXY);
//        xyPlot.getDomainLabelWidget().getLabelPaint().setTextSize(17);

        // Formato valores
        xyPlot.setRangeValueFormat(new DecimalFormat("#####"));
        // Limites 
        xyPlot.setRangeBoundaries(0, 11000, BoundaryMode.FIXED);
        // Paso 
        xyPlot.setRangeStep(XYStepMode.INCREMENT_BY_VAL, 1000);
        // Ticks
        xyPlot.setTicksPerRangeLabel(2);
        // Color
        xyPlot.getGraphWidget().getRangeOriginLinePaint().setColor(Color.DKGRAY);
        // Ancho
        xyPlot.getGraphWidget().getRangeOriginLinePaint().setStrokeWidth(2);
        // Fuente
        xyPlot.getGraphWidget().getRangeOriginLabelPaint().setTextSize(15);
        xyPlot.getGraphWidget().getRangeLabelPaint().setTextSize(15);
        xyPlot.getGraphWidget().getRangeOriginLabelPaint().setTypeface(tfXY);
        xyPlot.getGraphWidget().getRangeLabelPaint().setTypeface(tfXY);
        xyPlot.getGraphWidget().getRangeOriginLabelPaint().setColor(Color.WHITE);
        xyPlot.getGraphWidget().getRangeLabelPaint().setColor(Color.WHITE);
        
        //Color de fondo ejes
        xyPlot.getGraphWidget().getBackgroundPaint().setColor(Color.BLACK);
        
        
        /******************************Rejilla****************************/
        
        //Trazo rejilla
        xyPlot.getGraphWidget().getGridLinePaint().setPathEffect(
        	new DashPathEffect(new float[]{5, 10}, 1));
        //Color rejilla  
        xyPlot.getGraphWidget().getGridLinePaint().setColor(Color.DKGRAY);
        //Color de fondo rejilla 
        xyPlot.getGraphWidget().getGridBackgroundPaint().setColor(Color.BLACK);
        
        

        xyPlot.redraw();
 
	}

}
